/**
 流是JAVA 8API 添加的一个新的抽象 称为流stream。以一种声明性方式处理数据集合
 BasseStream 基础接口：声明了流管理的核心方法  （声明 =抽象 ，核心 =泛型接口）
 Stream：核心接口，声明了流操作的核心方法，其他接口为指定类型的适配  
 
 Stream的创建  常见5种方法
 1.stream创建
 Stream<Integer> stream1=stream.of(1,2,3,4,5);
 2.Collection集合创建 （常用）
 3.Array数组创建
 int[] intArr={1,23,4,5};
 IntSream arrayStream =arrays.stream(intArr);
 4.文件创建
 Stream<String > fileStream=Files.lines(paths.get("data.txt"),charset.defaultCharset());
 5.函数创建
 Consumer传参型 Stream<Integer> iterateStream=Stream.iterate(0,n->n+2).limt(5); 
 Supplier提供型 Stream<Double> generateStream=strea.generate(Math::random).limit(%);
 
 stream().中间操作符().终端操作符()
 
 1.形成流 ->通过各种操作  -> 得到想要的结果
 中间操作符
 filter
 map
 distinct
 sorted
 limit
 skip
 flatMap
 peek
 */
/*
package com.leo.第九章_集合;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class U9_L10_Stream接口的运用 {
	public static void main(String [] args) {
		stream_filter_collect(stream());
		stream_distinct(stream());
		stream_map(stream());
		stream_flatmap(stream());
		stream_objtostr(userlist1());
		stream_groupingBy(userlist1());
		stream_reduce(stream());
		stream_collect(stream());
	
	}
	private static Stream stream() {
		Stream<String> stream=Stream.of("1.aba","1.abc","1.qwe","2.ttq","2.ttq","3.wee","3.eec");
		return stream;
	}
	//filter 过滤规则
	private static void stream_filter_collect(Stream stream) {
		//。。将stream转成list
		List<String> list=(List<String>) stream.collect(Collectors.toList());
		//..list->stream->过滤->stream
		List <String> result=list.stream()		
				.filter(str->str.contains("1."))
				.collect(Collectors.toList());
		//collect 和foreach都是终端操作符 
		result.forEach(System.out::println);
		System.out.println("一个流只有一个终端操作符，使用完就关闭了，需要使用第二个就需要重新写一个流。");				
	}
	
	//distinct 去重
	private static void stream_distinct(Stream stream) {
		stream.distinct().forEach(str->System.out.println("2.distincat去重-----"+str));
	}
	
	//map 映射 包装类里面的方法可用于映射
	private static void stream_map(Stream <String> stream) {
		stream.map(str->str.toUpperCase()).forEach(str->System.out.println("3.map映射----"+str));
	}
	
	//flatmap  多层合一层
	private static void stream_flatmap(Stream <String> stream) {
		//流转list
		List<String> list=(List<String>) stream.collect(Collectors.toList());
		//2张list 放进流里 再打印每个表中的line。。。
		Stream<List<String>> stream1 =Stream.of(list,Arrays.asList("4.ww","4.we","5.ddf","5.ttt"));
		stream1.flatMap(list_str->list_str.stream())
		.forEach(str->System.out.println("4.flatmap多个列表映射成一张大的列表--"+str));
	}
	//reduce
	private static void stream_reduce(Stream <String> stream) {
		Optional<String> opt=stream.reduce((s1,s2)->s1.length()>=s2.length()?s1:s2);
		System.out.println(opt.get());
	}
	
	//collect
	private static void stream_collect(Stream<String> stream) {
		List<String> list=stream.collect(Collectors.toList());
		list.forEach((s)->System.out.println(s));
	}
	
	
	//终端操作符 collect的 里面的groupingby 操作分组条件
	private static int count_groupingBy=1;
	private static int count_groupingBy() {
		return count_groupingBy++;
	}
	private static void stream_groupingBy(List <User> userlist) {
		
		Map<String,List<User>> list=userlist.stream()
				.collect(Collectors.groupingBy(User::getColor));		
		list.forEach((key,value)->System.out.println(key+"-"+value+count_groupingBy()));
		
		
	}
	
	private static void stream_objtostr(List <User>userlist) {
		int count=0;
		List<String> list=userlist.stream()
				
				.filter(obj->!obj.getSex().equals("female"))
				.peek(userid->userid.setId(getNextId()))
				.map(User::toString)
				.collect(Collectors.toList());
				list.forEach(System.out::println);
		//Map<Integer,String> usermap=userlist.stream
				
	}
	
	private static List userlist1() {
		List userlist=new ArrayList();
		userlist.add(new User("小a",1,0,"male","yellow"));
		userlist.add(new User("小b",2,1,"female","white"));
		userlist.add(new User("小c",32,2,"male","black"));
		userlist.add(new User("小d",21,3,"female","red"));
		userlist.add(new User("小e",21,4,"male","yellow"));
		userlist.add(new User("小f",16,5,"female","yellow"));
		userlist.add(new User("小g",7,6,"male","yellow"));
		userlist.add(new User("小h",54,7,"female","black"));
		userlist.add(new User("小i",21,8,"female","black"));
		userlist.add(new User("小j",6,9,"male","yellow"));
		userlist.add(new User("小k",22,10,"male","black"));
		userlist.add(new User("小l",7,11,"female","red"));
		userlist.add(new User("小m",14,12,"female","yellow"));
		userlist.add(new User("小n",28,13,"female","red"));
		userlist.add(new User("小o",35,14,"female","red"));
		userlist.add(new User("小p",32,15,"female","yellow"));		 
		return userlist;
	}
	
	private static int count=0;
	private static int getNextId() {	
		return count++;
	}
}

class User{
	String name;
	int age;
	int id;
	String sex;
	String Color;
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", id=" + id + ", sex=" + sex + ", Color=" + Color + "]";
	}
	 
	public User(String name, int age, int id, String sex, String color) {
		super();
		this.name = name;
		this.age = age;
		this.id = id;
		this.sex = sex;
		Color = color;
	}
	public String getColor() {
		return Color;
	}
	public void setColor(String color) {
		Color = color;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
 
	public User(String name, int age, int id, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.id = id;
		this.sex = sex;
	}
	 
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
*/